package com.skyline.attraction.dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import com.skyline.attraction.dao.AttractionDao;
import com.skyline.attraction.mapper.AttractionMapper;
import com.skyline.attraction.model.Attraction;
import com.skyline.attraction.type.AttractionType;
import com.skyline.base.dao.impl.BaseDaoImpl;
import com.skyline.base.type.Activity;
import com.skyline.common.bean.Page;

@Repository("attractionDao")
public class AttractionDaoImpl extends BaseDaoImpl implements AttractionDao {
	/**
	 * insert into
	 * attraction(name,category,creatorId,creatorNickname,creatorPortrait
	 * ,creatorEmail
	 * ,adminId,adminNickname,adminPortrait,adminEmail,country,province
	 * ,city,siteId
	 * ,detailAddress,latitude,longitude,introduction,createTime,updateTime)
	 * values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,current_timestamp,
	 * current_timestamp)
	 */
	@Value("${AttractionDao.insertAttraction}")
	private String insertAttractionSql;
	/**
	 * update attraction set
	 * name=?,category=?,country=?,province=?,city=?,siteId
	 * =?,detailAddress=?,latitude
	 * =?,longitude=?,introduction=?,updateTime=current_timestamp where id=?
	 */
	@Value("${AttractionDao.updateAttraction}")
	private String updateAttractionSql;
	/**
	 * update attraction set introduction=?,updateTime=current_timestamp where
	 * id=?
	 */
	@Value("${AttractionDao.updateAttractionIntroduction}")
	private String updateAttractionIntroductionSql;

	/**
	 * update attraction set portrait=?,updateTime=current_timestamp where id=?
	 */
	@Value("${AttractionDao.updateAttractionPortrait}")
	private String updateAttractionPortraitSql;
	/**
	 * update attraction set star=?,votes=votes+1 where id=?
	 */
	@Value("${AttractionDao.updateAttractionStar}")
	private String updateAttractionStarSql;

	/**
	 * update attraction set visitnum=visitnum+? where id=?
	 */
	@Value("${AttractionDao.updateAttractionVisitNum}")
	private String updateAttractionVisitNumSql;
	/**
	 * update attraction set
	 * adminId=?,adminNickname=?,adminPortrait=?,adminEmail
	 * =?,updateTime=current_timestamp where id=?
	 */
	@Value("${AttractionDao.updateAdmin}")
	private String updateAdminSql;
	/**
	 * update attraction set activity=?,updatetime=current_timestamp where id=?
	 */
	@Value("${AttractionDao.updateAttractionActivity}")
	private String updateAttractionActivitySql;
	/**
	 * select * from attraction where activity=?
	 */
	@Value("${AttractionDao.queryAttractions}")
	private String queryAttractionsSql;
	/**
	 * select * from attraction where category=? and activity=?
	 */
	@Value("${AttractionDao.queryAttractionsByType}")
	private String queryAttractionsByTypeSql;

	/**
	 * select * from attraction where id=? and activity=?
	 */
	@Value("${AttractionDao.queryAttractionById}")
	private String queryAttractionByIdSql;

	/**
	 * select * from attraction where left(siteid,?)=? and activity=?
	 */
	@Value("${AttractionDao.queryAttractionsBySiteId}")
	private String queryAttractionsBySiteIdSql;
	/**
	 * select * from attraction where left(siteid,?)=? and category=? and
	 * activity=?
	 */
	@Value("${AttractionDao.queryAttractionsBySiteIdAndType}")
	private String queryAttractionsBySiteIdAndTypeSql;

	/**
	 * select * from attraction where name=? and activity=?
	 */
	@Value("${AttractionDao.queryAttractionsByName}")
	private String queryAttractionsByNameSql;
	/**
	 * select * from attraction where name=? and category=? and activity=?
	 */
	@Value("${AttractionDao.queryAttractionsByNameAndType}")
	private String queryAttractionsByNameAndTypeSql;

	@Override
	public Attraction queryAttractionById(Long attractionId, Activity activity) {
		List<Attraction> list = jdbcTemplate.query(queryAttractionByIdSql, AttractionMapper.getMapper(), attractionId,
				activity.toString());
		if (CollectionUtils.isEmpty(list)) {
			return null;
		} else {
			return list.get(0);
		}
	}

	@Override
	public List<Attraction> queryAttractions(Page page, Activity activity) {
		return getPaginationResult(queryAttractionsSql, page, AttractionMapper.getMapper(), Activity.NORMAL.toString());
	}

	@Override
	public List<Attraction> queryAttractionsByType(AttractionType type, Page page, Activity activity) {
		return this.getPaginationResult(queryAttractionsByTypeSql, page, AttractionMapper.getMapper(), type.toString(),
				activity.toString());
	}

	@Override
	public List<Attraction> queryAttractionsBySiteId(String siteId, int subLen, Page page, Activity activity) {
		return getPaginationResult(queryAttractionsBySiteIdSql, page, AttractionMapper.getMapper(), subLen, siteId,
				activity.toString());
	}

	@Override
	public List<Attraction> queryAttractionsBySiteIdAndType(String siteId, int subLen, AttractionType type, Page page,
			Activity activity) {
		return getPaginationResult(queryAttractionsBySiteIdAndTypeSql, page, AttractionMapper.getMapper(), subLen, siteId,
				type.toString(), activity.toString());
	}

	@Override
	public List<Attraction> queryAttractionsByNameAndType(String name, AttractionType type, Activity activity) {
		return jdbcTemplate.query(queryAttractionsByNameAndTypeSql, AttractionMapper.getMapper(), name,
				type.toString(), activity.toString());
	}

	@Override
	public List<Attraction> queryAttractionsByName(String name, Activity activity) {
		return jdbcTemplate.query(queryAttractionsByNameSql, AttractionMapper.getMapper(), name, activity.toString());
	}


	/*************************************** 更新and插入 *****************************************/
	@Override
	public long insertAttraction(String name, String portrait, AttractionType type, Long creatorId,
			String creatorNickName, String creatorPortrait, String creatorEmail, String country, String province,
			String city, String siteId, String detailAddress, Double lat, Double lng, String introduction) {
		return this.insertWithIdReturn(insertAttractionSql, name, portrait, type.toString(), creatorId,
				creatorNickName, creatorPortrait, creatorEmail, creatorId, creatorNickName, creatorPortrait,
				creatorEmail, country, province, city, siteId, detailAddress, lat, lng, introduction);
	}

	@Override
	public void updateAttractionIntroduction(Long attractionId, String introduction) {
		jdbcTemplate.update(updateAttractionIntroductionSql, introduction, attractionId);
	}

	@Override
	public void updateAttractionPortrait(Long attractionId, String portrait) {
		jdbcTemplate.update(updateAttractionPortraitSql, portrait, attractionId);
	}

	@Override
	public void updateAttractionStar(Long attractionId, float star) {
		jdbcTemplate.update(updateAttractionStarSql, star, attractionId);

	}

	@Override
	public void updateAttractionVisitNum(Long attractionId, Integer num) {
		jdbcTemplate.update(updateAttractionVisitNumSql, num, attractionId);
	}

	@Override
	public void updateAttractionAdmin(Long attractionId, Long adminId, String adminNickName, String adminEmail,
			String adminPortrait) {
		jdbcTemplate.update(updateAdminSql, adminId, adminNickName, adminPortrait, adminEmail, attractionId);

	}

	@Override
	public void updateAttraction(Long attractionId, String name, AttractionType type, String country, String province,
			String city, String siteId, String detailAddress, Double lat, Double lng, String introduction) {
		jdbcTemplate.update(updateAttractionSql, name, type.toString(), country, province, city, siteId, detailAddress,
				lat, lng, introduction, attractionId);
	}

	@Override
	public void updateAttractionActivity(Long attractionId, Activity activity) {
		jdbcTemplate.update(updateAttractionActivitySql, activity.toString(), attractionId);

	}


}
